package eu.vranckaert.calendar.test;

import android.app.Activity;
import android.os.Bundle;
import eu.vranckaert.calendar.CalendarDayView;
import eu.vranckaert.calendar.CalendarViewHelper;
import eu.vranckaert.calendar.util.TimeBlock;
import eu.vranckaert.calendar.widget.Workspace;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Date: 11/03/13
 * Time: 13:22
 *
 * @author Dirk Vranckaert
 */
public class DaysViewActivity extends Activity implements CalendarDayView.OnDataLoadListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.day_view);

        Workspace workspace = (Workspace) findViewById(R.id.workspace);
        CalendarViewHelper.initCalendarDay(this, workspace, new Date(), R.color.block_color, this);
    }

    @Override
    public List<MyTimeEntity> loadDataForDay(Date day) {
        List<MyTimeEntity> timeBlocks = new ArrayList<MyTimeEntity>();

        // Can be a database call here, for this demo we only display blocks on even days
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);

        if (calendar.get(Calendar.DAY_OF_MONTH) % 2 == 0) {
            MyTimeEntity te1 = new MyTimeEntity();
            te1.setStartTime(getTimeForDay(10, 0, day));
            te1.setEndTime(getTimeForDay(11, 30, day));
            MyTimeEntity te2 = new MyTimeEntity();
            te2.setStartTime(getTimeForDay(11, 30, day));
            te2.setEndTime(getTimeForDay(12, 0, day));
            MyTimeEntity te3 = new MyTimeEntity();
            te3.setStartTime(getTimeForDay(13, 0, day));
            te3.setEndTime(getTimeForDay(16, 0, day));
            MyTimeEntity te4 = new MyTimeEntity();
            te4.setStartTime(getTimeForDay(16, 45, day));
            te4.setEndTime(null); // This is a time block that will is not ended and thus will take the rest of the day,
                                  // not tomorrow

            timeBlocks.add(te1);
            timeBlocks.add(te2);
            timeBlocks.add(te3);
            timeBlocks.add(te4);
        }

        return timeBlocks;
    }

    private Date getTimeForDay(int hour, int minute, Date day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        return calendar.getTime();
    }
}
